package com.ape.controller;

import com.ape.pojo.Student;
import com.ape.pojo.query.StudentQuery;
import com.ape.service.IStudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private IStudentService iStudentService;

    @GetMapping("/")
    public String index(Model model, StudentQuery studentQuery){
        PageInfo<Student> studentPageInfo = iStudentService.listStudentByName(studentQuery);
        model.addAttribute("page",studentPageInfo);
        //往前台传数据，可以传对象，可以传List，通过el表达式 ${}可以获取到，
        // 类似于request.setAttribute("sts",sts)效果一样
        return "index";
    }

//    当表单提交的时候（搜索）时候需要走这个路径
    @PostMapping("/")
    public String listStudentByName(Model model, StudentQuery studentQuery){
        studentQuery.setSname(studentQuery.getSname().trim());//去掉输入的前后的空格
        PageInfo<Student> studentPageInfo = iStudentService.listStudentByName(studentQuery);
        model.addAttribute("page",studentPageInfo);
        return "index";
    }

    @GetMapping("delete/{sid}")
    public String delete(@PathVariable("sid") Integer sid, RedirectAttributes attributes){
        boolean b = iStudentService.deleteStudentById(sid);
        if (b){
            //如果删除成功需要重定向
            attributes.addFlashAttribute("message","删除用户成功");
            return "redirect:/";//重定向到首页
        }else {
            attributes.addFlashAttribute("message","删除用户失败");
            return "redirect:/";//重定向到首页
        }
    }

    //修改页面
    @GetMapping("/edit/{sid}")
    public String toEdit(@PathVariable Integer sid,Model model){
        model.addAttribute("student",iStudentService.queryStudentById(sid));
        return "editStudent";
    }

//    修改后的保存
    @PostMapping("/edit")//修改跟保存都走这个接口，得判断有无id,有id走修改，否则走保存
    public String edit(@Valid Student student, RedirectAttributes attributes, StudentQuery studentQuery){
        Integer sid = student.getSid();
        student.setSname(student.getSname());
//        要是用户已经名已存在，会提示“该用户名已存在”
        List<Student> studentPageInfo = iStudentService.queryStudentByName(student);
        if (sid != null){//修改
            //PageInfo<Student> studentPageInfo = iStudentService.listStudentByName(studentQuery);
            if (null == studentPageInfo || studentPageInfo.size() ==0 || studentPageInfo.get(0).getSid().equals(sid)){//表示当前集合没有值
                boolean b = iStudentService.updateStudent(student);
                if (b){
                    //如果删除成功需要重定向
                    attributes.addFlashAttribute("message","更新用户成功");
                    return "redirect:/";//重定向到首页
                }else {
                    attributes.addFlashAttribute("message","更新用户失败");
                    return "redirect:/";//重定向到首页
                }
            }else {
                attributes.addFlashAttribute("message","该用户名已存在！");
                return "redirect:/edit/"+student.getSid();
            }
        }else {//保存
            if (null == studentPageInfo || studentPageInfo.size() ==0){
                boolean b = iStudentService.addStudent(student);
                if (b){
                    //如果删除成功需要重定向
                    attributes.addFlashAttribute("message","新增用户成功");
                    return "redirect:/";//重定向到首页
                }else {
                    attributes.addFlashAttribute("message","新增用户失败");
                    return "redirect:/";//重定向到首页
                }
            }else {
                attributes.addFlashAttribute("message","新增失败,该用户名已存在！");
                return "redirect:/";
            }
        }
    }

    @GetMapping("/udate")
    public String Update(Model model){
        Student student = new Student();
        model.addAttribute("student",student);
        return "editStudent";
    }
}
